<template>
	<div class="sys-pos-container">
		<el-dialog v-model="state.isShowDialog" draggable :close-on-click-modal="false" width="700px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit />
					</el-icon>
					<span> {{ props.title }} </span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" class="fl">
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20" v-if="state.ruleForm.id">
					<el-form-item label="物料编号" prop="materielNum"
						:rules="[{ required: true, message: '物料编号不能为空', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.materielNum" placeholder="物料编号" disabled clearable />
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20" v-if="!state.ruleForm.id">
					<el-form-item label="物料编号" prop="materielNum"
						:rules="[{ required: true, message: '物料编号不能为空', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.materielNum" placeholder="物料编号" clearable />
					</el-form-item>
				</el-col>

				<el-form-item label="物料名称" prop="materielName"
					:rules="[{ required: true, message: '物料名称不能为空', trigger: 'blur' }]">
					<el-input v-model="state.ruleForm.materielName" placeholder="物料名称" clearable class="input" />
				</el-form-item>
				<el-form-item label="规格型号" prop="materielSpecs"
					:rules="[{ required: true, message: '规格型号不能为空', trigger: 'blur' }]">
					<el-input v-model="state.ruleForm.materielSpecs" placeholder="物料名称" clearable class="input" />
				</el-form-item>
				<!-- <el-form-item label="主单位">
					<el-select v-model="state.ruleForm.materielMainUnit" filterable clearable class="w100">
						<el-option v-for="item in state.MainUnit" :key="item" :label="item.value" :value="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="辅单位">
					<el-select v-model="state.ruleForm.materielAssistUnit" filterable clearable class="w100">
						<el-option v-for="item in state.AssistUnit" :key="item" :label="item.value"
							:value="item.value" />
					</el-select>
				</el-form-item> -->
				<!-- <el-form-item label="物料分类" prop="materielType"
					:rules="[{ required: true, message: '物料分类不能为空', trigger: 'blur' }]">
					<el-select v-model="state.ruleForm.materielType" filterable clearable class="w100">
						<el-option v-for="item in state.class" :key="item" :label="item.value" :value="item.value" />
					</el-select>
				</el-form-item> -->

				<el-form-item label="物料等级" prop="materielLevelNum">
					<el-select v-model="state.ruleForm.materielLevelNum" filterable clearable class="w100">
						<el-option v-for="item in state.grade" :key="item.materielLevelNum"
							:label="item.materielLevelName" :value="item.materielLevelNum" />

					</el-select>

				</el-form-item>
				<el-form-item label="安全库存">
					<el-input v-model="state.ruleForm.quantityNeedCount" placeholder="安全库存" clearable class="input" />
				</el-form-item>
				<el-form-item label="物料预警时间">
					<el-input v-model="state.ruleForm.inventoryDateTime" placeholder="物料预警时间" clearable class="input" />
				</el-form-item>
				<el-form-item label="物料备注">
					<el-input v-model="state.ruleForm.materielReamke" placeholder="物料备注" clearable class="input" />
				</el-form-item>
				<el-form-item label="物料来源">
					<el-input v-model="state.ruleForm.materielSource" placeholder="物料来源" clearable class="input" />
				</el-form-item>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="sysEditPos">
import { reactive, ref } from 'vue';

import { getAPI } from '/@/utils/axios-utils';
import { SysDictDataApi, SysPosApi } from '/@/api-services/api';
import { WmsApi } from '/@/api/wh';
import { ElMessage } from 'element-plus';

const props = defineProps({
	title: String,
	isEditing: Boolean
});
const emits = defineEmits(['handleQuery']);
const ruleFormRef = ref();
const state = reactive({
	isShowDialog: false,
	ruleForm: {

	},
	wTypes: [],
	grade:[]
});

// 打开弹窗
const openDialog = async (row: any) => {
	WmsApi().getAllClass({ page: 1, pagesize: 10 }).then(
      res => {
        console.log(res);
        state.grade =res.data.result.items
      }
    )
	var res = await getAPI(SysDictDataApi).apiSysDictDataDataListCodeGet('w_type');
	state.wTypes = res.data.result;
	state.ruleForm = JSON.parse(JSON.stringify(row));
	console.log(state.ruleForm.whStatus);

	state.isShowDialog = true;
	ruleFormRef.value?.resetFields();
};

// 关闭弹窗
const closeDialog = () => {
	emits('handleQuery');
	state.isShowDialog = false;


};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

// 提交
const submit = () => {
	ruleFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
		console.log(state.ruleForm.id);

		if (state.ruleForm.id) {
			console.log(state.ruleForm);

			await WmsApi().MaterielPagePut(state.ruleForm)
				.then(function (res) {
					ElMessage.success('修改成功');
					closeDialog();
				})
				.catch(function (error) {
					console.log(error);
				});
		} else {

			await WmsApi().MaterielPageAdd(state.ruleForm)
				.then(function (res) {
					ElMessage.success('新增成功');
					closeDialog();
				})
				.catch(function (error) {
					console.log(error);
				});

		}
	});
}

// 导出对象
defineExpose({ openDialog });
</script>
<style lang="scss" scoped>
.fl {
	width: 70%;
	margin: 20px auto;
}
</style>